package qj.tool.db;

import java.util.LinkedList;
import java.util.List;

import qj.tool.db.orientdb.Converter;
import qj.tool.db.orientdb.DBImpl;
import qj.tool.db.orientdb.KeyImpl;
import qj.util.ArrayUtil;
import qj.util.funct.F1;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.index.OIndex;

@SuppressWarnings("rawtypes")
public class DB extends DBImpl {

	public static final F1<byte[], Key> deserializeKeyB = new F1<byte[], Key>() {public Key e(byte[] bytes) {
		return new KeyImpl(new ORecordId().fromStream(bytes));
	}};

	public DB(ODatabaseDocumentTx ds) {
		super(ds);
	}

	public boolean indexContains(String value, String indexName) {
		OIndex<?> index = ds.getMetadata().getIndexManager().getIndex(indexName);
		return index.contains(value);
	}

	public boolean isClass(Key key, Class<?> clazz) {
		return ArrayUtil.contains(KeyImpl.idF.e(key).getClusterId(), 
				ds.getMetadata().getSchema().getClass(Converter.entityType(clazz)).getClusterIds());
	}

	public <A> List<A> getList(List<Key> ids, Class<A> clazz) {
		LinkedList<A> ret = new LinkedList<A>();
		for (Key key : ids) {
			ret.add( get(key, clazz) );
		}
		return ret;
	}

	public void close() {
		ds.close();
	}

//	public void saveAll(Collection<?> objects);
//
//	public <A> List<A> getAll(Class<A> clazz,Object... equals);
//	public <A> A get(Key key, Class<A> clazz);
//	public String serializeKey(Key key);
//	public Key deserializeKey(String keyStr);
//
//	void begin();
//
//	void commit();
//
//	void rollback();
//
//	public void save(Object o);
//
//	public <A> void delete(A item);
//	public void delete(Key o);
}
